package com.ruoyi.clinic.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.clinic.domain.*;
import com.ruoyi.clinic.dto.clinicorg.*;
import com.ruoyi.clinic.mapper.ClinicCityMapper;
import com.ruoyi.clinic.mapper.ClinicDistrictMapper;
import com.ruoyi.clinic.mapper.ClinicProvinceMapper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.ClinicConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.html.EscapeUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.clinic.mapper.ClinicOrgInfoMapper;
import com.ruoyi.clinic.service.IClinicOrgInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 机构信息Service业务层处理
 *
 * @author zhangjinfei
 * @date 2025-11-22
 */
@Service
@Slf4j
public class ClinicOrgInfoServiceImpl implements IClinicOrgInfoService {

    @Autowired
    private ClinicOrgInfoMapper clinicOrgInfoMapper;

    @Autowired
    private ClinicProvinceMapper clinicProvinceMapper;

    @Autowired
    private ClinicCityMapper clinicCityMapper;

    @Autowired
    private ClinicDistrictMapper clinicDistrictMapper;

    @Autowired
    protected Validator validator;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询机构信息
     *
     * @param orgId 机构信息主键
     * @return 机构信息
     */
    @Override
    public ClinicOrgInfo selectClinicOrgInfoByOrgId(Long orgId) {
        return clinicOrgInfoMapper.selectClinicOrgInfoByOrgId(orgId);
    }

    /**
     * 查询机构信息列表
     *
     * @param clinicOrgInfo 机构信息
     * @return 机构信息
     */
    @Override
    public List<ClinicOrgInfo> selectClinicOrgInfoList(ClinicOrgInfo clinicOrgInfo) {
        return clinicOrgInfoMapper.selectClinicOrgInfoList(clinicOrgInfo);
    }

    @Override
    public ClinicOrgDetailDTO queryClinicDetail(Long orgId, SysUser sysUser) {
        ClinicOrgInfo clinicOrgInfo = clinicOrgInfoMapper.selectClinicOrgInfoByOrgId(orgId);
        if (null == clinicOrgInfo) {
            return null;
        }
        ClinicOrgDetailDTO clinicOrgDetailDTO = BeanUtil.copyProperties(clinicOrgInfo, ClinicOrgDetailDTO.class);
        return this.convertClinicOrgDetailDTO(clinicOrgDetailDTO, sysUser);
    }

    /**
     * 结果集处理
     *
     * @param clinicOrgDetailDTO 机构详情
     * @param sysUser            当前用户
     * @return 处理对象
     */
    private ClinicOrgDetailDTO convertClinicOrgDetailDTO(ClinicOrgDetailDTO clinicOrgDetailDTO, SysUser sysUser) {
        String isShowSensitivePhone = sysUser.getIsShowSensitivePhone();
        // 1.只有admin用户可以查看电话号码，其他用户根据系统设置可以查看
        if (!sysUser.isAdmin()) {
            clinicOrgDetailDTO.setTelephone(ClinicConstants.YesOrNoEnum._YES.getType().equals(isShowSensitivePhone) ? clinicOrgDetailDTO.getTelephone() : ClinicConstants.MASK);
        }
        return clinicOrgDetailDTO;
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<ClinicOrgResDTO> queryPageList(ClinicOrgReqDTO dto, SysUser sysUser) {
        List<ClinicOrgResDTO> clinicOrgResDTOList = clinicOrgInfoMapper.queryPageList(dto);
        return this.convertClinicOrgResDTOList(clinicOrgResDTOList, sysUser);
    }

    /**
     * 机构结果集转换
     *
     * @param clinicOrgResDTOList 查询结果集
     * @param sysUser             当前登录用户
     * @return 处理结果
     */
    private List<ClinicOrgResDTO> convertClinicOrgResDTOList(List<ClinicOrgResDTO> clinicOrgResDTOList, SysUser sysUser) {
        String isShowSensitivePhone = sysUser.getIsShowSensitivePhone();
        for (ClinicOrgResDTO dto : clinicOrgResDTOList) {
            // 1.只有admin用户可以查看电话号码，其他用户根据系统设置可以查看
            if (!sysUser.isAdmin()) {
                dto.setTelephone(ClinicConstants.YesOrNoEnum._YES.getType().equals(isShowSensitivePhone) ? dto.getTelephone() : ClinicConstants.MASK);
            }
        }
        return clinicOrgResDTOList;
    }

    @Override
    public List<ClinicOrgResDTO> queryMyOrgPageList(ClinicOrgReqDTO dto, SysUser sysUser) {
        List<ClinicOrgResDTO> clinicOrgResDTOList = clinicOrgInfoMapper.queryMyOrgPageList(dto);
        return this.convertClinicOrgResDTOList(clinicOrgResDTOList, sysUser);
    }

    @Override
    public String importData(List<ClinicOrgImportDTO> importDTOList, boolean updateSupport, SysUser sysUser) {
        if (StringUtils.isNull(importDTOList) || importDTOList.isEmpty()) {
            throw new ServiceException("导入机构数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        // 根据 机构名称查询
        List<String> orgNameList = importDTOList.stream().map(ClinicOrgImportDTO::getOrgName).toList();
        List<ClinicOrgInfo> clinicOrgInfos = this.clinicOrgInfoMapper.selectClinicOrgInfoListByOrgNames(orgNameList);
        Map<String, ClinicOrgInfo> clinicOrgInfoMap = clinicOrgInfos.stream().collect(Collectors.toMap(ClinicOrgInfo::getOrgName, val -> val, (v1, v2) -> v2));
        // 根据 省份名称查询
        List<String> provinceNameList = importDTOList.stream().map(ClinicOrgImportDTO::getProvinceName).toList();
        List<ClinicProvince> clinicProvinces = this.clinicProvinceMapper.selectListByNames(provinceNameList);
        Map<String, ClinicProvince> clinicProvinceMap = clinicProvinces.stream().collect(Collectors.toMap(ClinicProvince::getName, val -> val, (v1, v2) -> v2));
        // 根据 城市名称查询
        List<String> cityNameList = importDTOList.stream().map(ClinicOrgImportDTO::getCityName).toList();
        List<ClinicCity> clinicCities = this.clinicCityMapper.selectListByNames(cityNameList);
        Map<String, ClinicCity> clinicCitieMap = clinicCities.stream().collect(Collectors.toMap(ClinicCity::getName, val -> val, (v1, v2) -> v2));
        // 根据 县区名称查询
        List<String> districtNameList = importDTOList.stream().map(ClinicOrgImportDTO::getDistrictName).toList();
        List<ClinicDistrict> clinicDistricts = this.clinicDistrictMapper.selectListByNames(districtNameList);
        Map<String, ClinicDistrict> clinicDistrictMap = clinicDistricts.stream().collect(Collectors.toMap(ClinicDistrict::getName, val -> val, (v1, v2) -> v2));
        for (ClinicOrgImportDTO importDTO : importDTOList) {
            String orgName = importDTO.getOrgName();
            try {
                // 入参非空校验
                BeanValidators.validateWithException(validator, importDTO);
                // 入参数据格式校验
                this.checkClinicOrgImportDTO(importDTO);
                ClinicProvince clinicProvince = clinicProvinceMap.get(importDTO.getProvinceName());
                if (null == clinicProvince) {
                    throw new ServiceException(String.format("根据所在省份名称【%s】，获取不到对应省份", importDTO.getProvinceName()));
                }
                ClinicCity clinicCity = clinicCitieMap.get(importDTO.getCityName());
                if (null == clinicCity) {
                    throw new ServiceException(String.format("根据所在城市名称【%s】，获取不到对应城市", importDTO.getCityName()));
                }
                ClinicDistrict clinicDistrict = clinicDistrictMap.get(importDTO.getDistrictName());
                if (null == clinicDistrict) {
                    throw new ServiceException(String.format("根据所在县区名称【%s】，获取不到对应县区", importDTO.getDistrictName()));
                }
                // 验证合法性
                ClinicOrgInfo clinicOrgInfo = clinicOrgInfoMap.get(importDTO.getOrgName());
                if (null == clinicOrgInfo) {
                    // 校验数据权限
                    deptService.checkDeptDataScope(importDTO.getCreateDept()); // 校验部门数据权限
                    ClinicOrgInfo addClinicOrgInfo = this.buildClinicOrgInfo(importDTO, clinicProvinceMap, clinicCitieMap, clinicDistrictMap);
                    this.clinicOrgInfoMapper.insertClinicOrgInfo(addClinicOrgInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、机构名称 " + orgName + " 导入成功");
                } else if (updateSupport) {
                    // 校验数据权限
                    userService.checkUserDataScope(importDTO.getUpdateById());  // 校验用户数据权限
                    deptService.checkDeptDataScope(importDTO.getCreateDept());  // 校验部门数据权限
                    ClinicOrgInfo updateClinicOrgInfo = this.buildClinicOrgInfo(importDTO, clinicProvinceMap, clinicCitieMap, clinicDistrictMap);
                    updateClinicOrgInfo.setOrgId(clinicOrgInfo.getOrgId());
                    this.clinicOrgInfoMapper.updateClinicOrgInfo(updateClinicOrgInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、机构名称 " + orgName + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、机构名称 " + orgName + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class)) {
                    orgName = EscapeUtil.clean(orgName);
                }
                String msg = "<br/>" + failureNum + "、机构名称 " + orgName + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 根据导入的数据，构建ClinicOrgInfo
     *
     * @param importDTO         导入的数据
     * @param clinicProvinceMap 省份
     * @param clinicCitieMap    城市
     * @param clinicDistrictMap 县区
     * @return ClinicOrgInfo
     */
    private ClinicOrgInfo buildClinicOrgInfo(ClinicOrgImportDTO importDTO,
                                             Map<String, ClinicProvince> clinicProvinceMap,
                                             Map<String, ClinicCity> clinicCitieMap,
                                             Map<String, ClinicDistrict> clinicDistrictMap) {
        ClinicOrgInfo clinicOrgInfo = BeanUtil.copyProperties(importDTO, ClinicOrgInfo.class);
        ClinicProvince clinicProvince = clinicProvinceMap.get(importDTO.getProvinceName());
        ClinicCity clinicCity = clinicCitieMap.get(importDTO.getCityName());
        ClinicDistrict clinicDistrict = clinicDistrictMap.get(importDTO.getDistrictName());
        if (null != clinicProvince) {
            clinicOrgInfo.setProvinceId(clinicProvince.getId());
        } else {
            clinicOrgInfo.setProvinceId(null);
            clinicOrgInfo.setProvinceName(null);
        }
        if (null != clinicCity) {
            clinicOrgInfo.setCityId(clinicCity.getId());
        } else {
            clinicOrgInfo.setCityId(null);
            clinicOrgInfo.setCityName(null);
        }
        if (null != clinicDistrict) {
            clinicOrgInfo.setDistrictId(clinicDistrict.getId());
        } else {
            clinicOrgInfo.setDistrictId(null);
            clinicOrgInfo.setDistrictName(null);
        }
        return clinicOrgInfo;
    }

    /**
     * 检查导入数据
     *
     * @param importDTO 导入数据
     */
    private void checkClinicOrgImportDTO(ClinicOrgImportDTO importDTO) {
        String orgCategory = importDTO.getOrgCategory();
        List<String> orgCategoryList = Arrays.asList(ClinicConstants.OrgCategory._TERMINAL_COMPANY.getCategory(),
                ClinicConstants.OrgCategory._AGENCY_COMPANY.getCategory(),
                ClinicConstants.OrgCategory._PERSONAL_AGENCY.getCategory());
        if (!orgCategoryList.contains(importDTO.getOrgCategory())) {
            throw new ServiceException("机构类别，格式不正确");
        }
    }

    @Override
    public String importMyOrgData(List<ClinicOrgImportDTO> importDTOList, boolean updateSupport, SysUser sysUser) {
        if (StringUtils.isNull(importDTOList) || importDTOList.isEmpty()) {
            throw new ServiceException("导入机构数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        // 根据 机构名称查询
        List<String> orgNameList = importDTOList.stream().map(ClinicOrgImportDTO::getOrgName).toList();
        List<ClinicOrgInfo> clinicOrgInfos = this.clinicOrgInfoMapper.selectClinicOrgInfoListByOrgNames(orgNameList);
        Map<String, ClinicOrgInfo> clinicOrgInfoMap = clinicOrgInfos.stream().collect(Collectors.toMap(ClinicOrgInfo::getOrgName, val -> val, (v1, v2) -> v2));
        // 根据 省份名称查询
        List<String> provinceNameList = importDTOList.stream().map(ClinicOrgImportDTO::getProvinceName).toList();
        List<ClinicProvince> clinicProvinces = this.clinicProvinceMapper.selectListByNames(provinceNameList);
        Map<String, ClinicProvince> clinicProvinceMap = clinicProvinces.stream().collect(Collectors.toMap(ClinicProvince::getName, val -> val, (v1, v2) -> v2));
        // 根据 城市名称查询
        List<String> cityNameList = importDTOList.stream().map(ClinicOrgImportDTO::getCityName).toList();
        List<ClinicCity> clinicCities = this.clinicCityMapper.selectListByNames(cityNameList);
        Map<String, ClinicCity> clinicCitieMap = clinicCities.stream().collect(Collectors.toMap(ClinicCity::getName, val -> val, (v1, v2) -> v2));
        // 根据 县区名称查询
        List<String> districtNameList = importDTOList.stream().map(ClinicOrgImportDTO::getDistrictName).toList();
        List<ClinicDistrict> clinicDistricts = this.clinicDistrictMapper.selectListByNames(districtNameList);
        Map<String, ClinicDistrict> clinicDistrictMap = clinicDistricts.stream().collect(Collectors.toMap(ClinicDistrict::getName, val -> val, (v1, v2) -> v2));
        for (ClinicOrgImportDTO importDTO : importDTOList) {
            String orgName = importDTO.getOrgName();
            try {
                // 入参非空校验
                BeanValidators.validateWithException(validator, importDTO);
                // 入参数据格式校验
                this.checkClinicOrgImportDTO(importDTO);
                ClinicProvince clinicProvince = clinicProvinceMap.get(importDTO.getProvinceName());
                if (null == clinicProvince) {
                    throw new ServiceException(String.format("根据所在省份名称【%s】，获取不到对应省份", importDTO.getProvinceName()));
                }
                ClinicCity clinicCity = clinicCitieMap.get(importDTO.getCityName());
                if (null == clinicCity) {
                    throw new ServiceException(String.format("根据所在城市名称【%s】，获取不到对应城市", importDTO.getCityName()));
                }
                ClinicDistrict clinicDistrict = clinicDistrictMap.get(importDTO.getDistrictName());
                if (null == clinicDistrict) {
                    throw new ServiceException(String.format("根据所在县区名称【%s】，获取不到对应县区", importDTO.getDistrictName()));
                }
                // 验证合法性
                ClinicOrgInfo clinicOrgInfo = clinicOrgInfoMap.get(importDTO.getOrgName());
                if (null == clinicOrgInfo) {
                    // 校验数据权限
                    deptService.checkDeptDataScope(importDTO.getCreateDept()); // 校验部门数据权限
                    ClinicOrgInfo addClinicOrgInfo = this.buildClinicOrgInfo(importDTO, clinicProvinceMap, clinicCitieMap, clinicDistrictMap);
                    this.clinicOrgInfoMapper.insertClinicOrgInfo(addClinicOrgInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、机构名称 " + orgName + " 导入成功");
                } else if (updateSupport) {
                    // 校验数据权限
                    userService.checkUserDataScope(importDTO.getUpdateById());  // 校验用户数据权限
                    // 校验是否是我负责的医生
                    Long createById = clinicOrgInfo.getCreateById();
                    Long createDept = clinicOrgInfo.getCreateDept();
                    if (!(importDTO.getUpdateById().equals(createById) && importDTO.getCreateDept().equals(createDept))) {
                        throw new ServiceException(String.format("机构名称 %s 不是当前用户所负责的，不支持修改", orgName));
                    }
                    ClinicOrgInfo updateClinicOrgInfo = this.buildClinicOrgInfo(importDTO, clinicProvinceMap, clinicCitieMap, clinicDistrictMap);
                    updateClinicOrgInfo.setOrgId(clinicOrgInfo.getOrgId());
                    this.clinicOrgInfoMapper.updateClinicOrgInfo(updateClinicOrgInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、机构名称 " + orgName + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、机构名称 " + orgName + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class)) {
                    orgName = EscapeUtil.clean(orgName);
                }
                String msg = "<br/>" + failureNum + "、机构名称 " + orgName + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 新增机构信息
     *
     * @param dto 机构信息
     * @return 结果
     */
    @Override
    public int insertClinicOrgInfo(ClinicOrgUpdateDTO dto) {
        dto.setCreateTime(DateUtils.getNowDate());
        dto.setUpdateTime(DateUtils.getNowDate());
        // 1.校验
        this.checkCreate(dto);
        // 根据省份ID和城市ID，县区ID，查询名称
        ClinicProvince clinicProvince = clinicProvinceMapper.selectClinicProvinceById(dto.getProvinceId());
        ClinicCity clinicCity = clinicCityMapper.selectClinicCityById(dto.getCityId());
        ClinicDistrict clinicDistrict = clinicDistrictMapper.selectClinicDistrictById(dto.getDistrictId());
        // 2.新增
        this.doCreate(dto, clinicProvince, clinicCity, clinicDistrict);
        return ClinicConstants.ONE_INT;
    }

    /**
     * 新增交易逻辑
     *
     * @param dto 前端入参
     */
    private void checkCreate(ClinicOrgUpdateDTO dto) {
        // 机构名称是否重复
        List<ClinicOrgInfo> clinicOrgInfos = clinicOrgInfoMapper.selectClinicOrgInfoListByOrgName(dto.getOrgName());
        if (CollectionUtil.isNotEmpty(clinicOrgInfos)) {
            throw new ServiceException(String.format("新增机构信息，机构名称【%s】已存在", dto.getOrgName()));
        }
    }

    /**
     * 新增机构信息，返回主键ID
     *
     * @param dto            前端入参
     * @param clinicProvince 省份
     * @param clinicCity     城市
     * @param clinicDistrict 县区
     * @return 主键ID
     */
    private Long doCreate(ClinicOrgUpdateDTO dto, ClinicProvince clinicProvince, ClinicCity clinicCity, ClinicDistrict clinicDistrict) {
        ClinicOrgInfo clinicOrgInfo = new ClinicOrgInfo();
        BeanUtils.copyProperties(dto, clinicOrgInfo);
        clinicOrgInfo.setProvinceName(null != clinicProvince ? clinicProvince.getName() : null);
        clinicOrgInfo.setCityName(null != clinicCity ? clinicCity.getName() : null);
        clinicOrgInfo.setDistrictName(null != clinicDistrict ? clinicDistrict.getName() : null);
        clinicOrgInfoMapper.insertClinicOrgInfo(clinicOrgInfo);
        return clinicOrgInfo.getOrgId();
    }

    /**
     * 修改机构信息
     *
     * @param dto 机构信息
     * @return 结果
     */
    @Override
    public int updateClinicOrgInfo(ClinicOrgUpdateDTO dto) {
        dto.setUpdateTime(DateUtils.getNowDate());
        // 1.校验
        this.checkUpdate(dto);
        // 根据省份ID和城市ID，县区ID，查询名称
        ClinicProvince clinicProvince = clinicProvinceMapper.selectClinicProvinceById(dto.getProvinceId());
        ClinicCity clinicCity = clinicCityMapper.selectClinicCityById(dto.getCityId());
        ClinicDistrict clinicDistrict = clinicDistrictMapper.selectClinicDistrictById(dto.getDistrictId());
        // 2.新增
        this.doUpdate(dto, clinicProvince, clinicCity, clinicDistrict);
        return ClinicConstants.ONE_INT;
    }

    /**
     * 更新机构信息检查
     *
     * @param dto 前端入参
     */
    private void checkUpdate(ClinicOrgUpdateDTO dto) {
        if (null == dto.getOrgId()) {
            throw new ServiceException("修改机构信息，参数有误");
        }
        List<ClinicOrgInfo> clinicOrgInfos = clinicOrgInfoMapper.selectClinicOrgInfoListByOrgName(dto.getOrgName());
        if (CollectionUtil.isNotEmpty(clinicOrgInfos)) {
            List<ClinicOrgInfo> filterClinicOrgInfos = clinicOrgInfos.stream().filter(one -> !one.getOrgId().equals(dto.getOrgId())).toList();
            if (CollectionUtil.isNotEmpty(filterClinicOrgInfos)) {
                throw new ServiceException(String.format("编辑机构信息，机构名称【%s】已存在", dto.getOrgName()));
            }
        }
    }

    /**
     * 修改机构信息，返回主键ID
     *
     * @param dto            前端入参
     * @param clinicProvince 省份
     * @param clinicCity     城市
     * @param clinicDistrict 县区
     * @return 主键ID
     */
    private void doUpdate(ClinicOrgUpdateDTO dto, ClinicProvince clinicProvince, ClinicCity clinicCity, ClinicDistrict clinicDistrict) {
        ClinicOrgInfo clinicOrgInfo = new ClinicOrgInfo();
        BeanUtils.copyProperties(dto, clinicOrgInfo);
        clinicOrgInfo.setProvinceName(null != clinicProvince ? clinicProvince.getName() : null);
        clinicOrgInfo.setCityName(null != clinicCity ? clinicCity.getName() : null);
        clinicOrgInfo.setDistrictName(null != clinicDistrict ? clinicDistrict.getName() : null);
        // 电话
        clinicOrgInfo.setTelephone(ClinicConstants.MASK.equals(dto.getTelephone()) ? null : dto.getTelephone());
        clinicOrgInfoMapper.updateClinicOrgInfo(clinicOrgInfo);
    }

    /**
     * 批量删除机构信息
     *
     * @param orgIds 需要删除的机构信息主键
     * @return 结果
     */
    @Override
    public int deleteClinicOrgInfoByOrgIds(String orgIds) {
        return clinicOrgInfoMapper.deleteClinicOrgInfoByOrgIds(Convert.toStrArray(orgIds));
    }

    /**
     * 删除机构信息信息
     *
     * @param orgId 机构信息主键
     * @return 结果
     */
    @Override
    public int deleteClinicOrgInfoByOrgId(Long orgId) {
        return clinicOrgInfoMapper.deleteClinicOrgInfoByOrgId(orgId);
    }
}
